package com.bases.ScollBar
{
	import com.bases.ImageButton;
	import com.bases.TUIComponent;
	import com.Timer.FrameTimer;
	import com.Timer.FrameTimerEvent;
	import com.debug.TDebug;
	import com.skin.SkinManage;
	
	import config.TConfig;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	
	
	public class TVerScrollBar extends TUIComponent {
		private var controlTarget:Sprite;
		private var speed:Number = 10;
		private var xMin:Number=0;
		private var xMax:Number=0;
		private var targetWidth:Number=0;
		public var maskWidth:Number;
		
		protected var btnLeft:ImageButton;
		protected var btnRight:ImageButton;
		protected var btnVerTrack:ImageButton;
		protected var btnVerScroll:ImageButton;
		//是否按下两个按钮(向上或向下)
		protected var isBtnDown:Boolean=false;
		//计时(用于鼠标放在两个按钮上)
		protected var frameTimer:FrameTimer;
		//鼠标放在按钮上面
		protected var target:Sprite;
		//down:为点按下按钮,up:为点按上按钮
		protected var type:String='';
		protected var isMouseUp:Boolean=true;
		public function TVerScrollBar(_target:Sprite) {
			super();
			this.controlTarget=_target;
			this.xMin=TBaseScrollBar.BUTTON_WIDTH;
			btnLeft=new ImageButton(SkinManage.getInstance().getClass('Scrollleft'),SkinManage.getInstance().getClass('Scrollleft'),SkinManage.getInstance().getClass('Scrollleft'));
			btnRight=new ImageButton(SkinManage.getInstance().getClass('ScroolRight'),SkinManage.getInstance().getClass('ScroolRight'),SkinManage.getInstance().getClass('ScroolRight'));
			btnVerTrack=new ImageButton(SkinManage.getInstance().getClass('ScrollVerTrack'),SkinManage.getInstance().getClass('ScrollVerTrack'),SkinManage.getInstance().getClass('ScrollVerTrack'));
			btnVerScroll=new ImageButton(SkinManage.getInstance().getClass('scroolVerScrool'),SkinManage.getInstance().getClass('scroolVerScrool'),SkinManage.getInstance().getClass('scroolVerScroolOver'));
			this.addChilds(btnLeft,btnVerTrack,btnVerScroll,btnRight);
			
			this.btnLeft.addEventListener(MouseEvent.MOUSE_DOWN,onLeft);
			this.btnRight.addEventListener(MouseEvent.MOUSE_DOWN,onRight);
			this.btnVerTrack.addEventListener(MouseEvent.MOUSE_DOWN,onVerTrack);
			this.btnVerScroll.addEventListener(MouseEvent.MOUSE_DOWN,onVerScroll);
			this.relayout();
			frameTimer=new FrameTimer(200,0);
			frameTimer.addEventListener(FrameTimerEvent.TIMER,onTimer);
		}
		
		override protected function relayout():void{
			this.btnLeft.setPos(0,0);
			this.btnVerTrack.setPos(TBaseScrollBar.BUTTON_HEIGHT,0);
			this.btnVerScroll.setPos(TBaseScrollBar.BUTTON_HEIGHT,0);
			this.btnVerScroll.setScale9Grid(5,5,25,8);
			this.btnVerTrack.setSize(30,TBaseScrollBar.BUTTON_HEIGHT);
			this.btnRight.setPos(0,0);
			this.btnVerScroll.setSize(TBaseScrollBar.BUTTON_WIDTH*2,TBaseScrollBar.BUTTON_HEIGHT);
		}
		
		public function invalidateScrollBarWidth(_width:Number):void{
			var isVScroll:Boolean=_width>maskWidth;
			targetWidth=_width;
			if (isVScroll)
			{
				this.btnVerTrack.setSize(maskWidth-2*TBaseScrollBar.BUTTON_WIDTH,this.btnVerTrack.height);
				var scrollWidth:Number=(this.btnVerTrack.width)*(1-(_width-maskWidth)/_width);
				this.btnVerScroll.setSize(scrollWidth,this.btnVerScroll.height);
				this.btnVerScroll.x=this.btnLeft.x+TBaseScrollBar.BUTTON_WIDTH;
				this.btnRight.x=this.btnVerTrack.x+this.btnVerTrack.width;
				
				this.xMax=this.btnRight.x-this.btnVerScroll.width;
				this.visible=true;
				thumbScrollH();//改变位置
			}
			else
			{
				this.controlTarget.x=0;
				this.visible=false;
			}
		}
		
		public function onLeft(event:MouseEvent):void{
			this.btnVerScroll.x-=this.speed;
			if(this.btnVerScroll.x<=xMin){
				btnVerScroll.x=xMin;
			}
			this.addTargetEvent(this.btnLeft,'up');
			thumbScrollH();
		}
		
		public function onRight(event:MouseEvent):void{
			this.btnVerScroll.x+=this.speed;
			if(this.btnVerScroll.x>=xMax){
				this.btnVerScroll.x=xMax;
			}
			thumbScrollH();
			this.addTargetEvent(this.btnLeft,'down');
		}
		
		public function inivalidate():void{
			var scrollPercent:Number=-this.controlTarget.x/(targetWidth - maskWidth);
			btnVerScroll.x=scrollPercent*(xMax - TBaseScrollBar.BUTTON_HEIGHT)+TBaseScrollBar.BUTTON_HEIGHT;
		}
		
		public function onVerTrack(event:MouseEvent):void{
			//点击非滑动条的位置
			var nowPosition : Number;
			if ((mouseX - this.btnLeft.x) < (this.btnVerScroll.width / 2))
			{
				nowPosition = Math.floor(this.btnLeft.x +TBaseScrollBar.BUTTON_WIDTH);
			}
			else if ((this.btnRight.x - mouseX) < btnVerScroll.width / 2)
			{
				nowPosition = Math.floor(btnRight.x - btnVerScroll.width);
			}
			else
			{
				nowPosition = mouseX - btnVerScroll.width / 2;
			}
			btnVerScroll.x = nowPosition;
		}
		
		public function onVerScroll(event:MouseEvent):void{
			btnVerScroll.startDrag(false,new Rectangle(TBaseScrollBar.BUTTON_WIDTH,0,this.width-2*TBaseScrollBar.BUTTON_WIDTH-btnVerScroll.width,0));
			isMouseUp=false;
			btnVerScroll.addEventListener(MouseEvent.MOUSE_UP,onVerMouseUp);
			TConfig.getInstance().mainSprite.addEventListener(MouseEvent.MOUSE_MOVE,onStateVerMove);
			TConfig.getInstance().mainSprite.addEventListener(MouseEvent.MOUSE_UP,onVerMouseUp);
		}
		
		//鼠标弹起,或鼠标离开
		protected function onVerMouseUp(event:MouseEvent):void{
			if(!isMouseUp){
				isMouseUp=true;
				btnVerScroll.stopDrag();
				btnVerScroll.removeEventListener(MouseEvent.MOUSE_UP,onVerMouseUp);
				TConfig.getInstance().mainSprite.removeEventListener(MouseEvent.MOUSE_MOVE,onStateVerMove);
				TConfig.getInstance().mainSprite.removeEventListener(MouseEvent.MOUSE_UP,onStageMouseUp);
			}
		}
		
		protected function onStageMouseUp(event:MouseEvent):void{
			this.onVerMouseUp(event);	
		}
		
		
		protected function thumbScrollH():void{
			var scrollPercent:Number=(btnVerScroll.x - TBaseScrollBar.BUTTON_HEIGHT) / (xMax - TBaseScrollBar.BUTTON_HEIGHT);
			this.controlTarget.x= - scrollPercent * (targetWidth - maskWidth);
		}
		
		protected function onStateVerMove(event:MouseEvent):void{
			this.thumbScrollH();
		}
		
		protected function addTargetEvent(_target:Sprite,type:String):void{
			this.target=_target;
			isBtnDown=true;
			this.type=type;
			this.frameTimer.start();
			TConfig.getInstance().mainSprite.addEventListener(MouseEvent.MOUSE_UP,onTargetUp);
			target.addEventListener(MouseEvent.MOUSE_UP,onTargetUp);
			this.target.addEventListener(MouseEvent.MOUSE_OUT,onDownMouseOut);
		}
		
		protected function onTargetUp(event:MouseEvent):void{
			if(isBtnDown){
				isBtnDown=false;
				this.frameTimer.stop();
				this.target.removeEventListener(MouseEvent.MOUSE_UP,onTargetUp);
				TConfig.getInstance().mainSprite.removeEventListener(MouseEvent.MOUSE_UP,onTargetUp);
			}
		}
		
		protected function onDownMouseOut(event:MouseEvent):void{
			this.frameTimer.stop();
		}
		
		protected function onTimer(event:FrameTimerEvent):void{
			if(type=='down'){
				btnVerScroll.x+=this.speed;
				if(this.btnVerScroll.x>=xMax){
					btnVerScroll.x=xMax;
				}
			}else{
				btnVerScroll.x-= this.speed;
				if(this.btnVerScroll.x<=this.xMin){
					this.btnVerScroll.x=this.xMin;
				}
			}
			this.thumbScrollH();
		}
		
		override public function dispose():void{
			if(!isDispose){
				super.dispose();
				this.btnLeft.removeEventListener(MouseEvent.MOUSE_DOWN,onLeft);
				this.btnRight.removeEventListener(MouseEvent.MOUSE_DOWN,onRight);
				this.btnVerTrack.removeEventListener(MouseEvent.MOUSE_DOWN,onVerTrack);
				this.btnVerScroll.removeEventListener(MouseEvent.MOUSE_DOWN,onVerScroll);
				btnLeft=null;
				this.btnRight=null;
				this.btnVerTrack=null;
				this.btnVerScroll=null;
				controlTarget=null;
				frameTimer.removeEventListener(FrameTimerEvent.TIMER,onTimer);
				frameTimer.dispose();
				frameTimer=null;
			}
		}
	}

}
